//
//  StrategyCollectionView.m
//  BeautifulShoppings
//
//  Created by GivenBmli on 16/9/5.
//  Copyright © 2016年 GivenBmli. All rights reserved.
//

#import "StrategyCollectionView.h"
#import "StrategyCollectionViewCell.h"
#import "NetworkHandler.h"
#import "TAMacro.h"
#import "StrategyUpModel.h"
@implementation StrategyCollectionView
-(NSMutableArray *)Array{
    if (!_Array) {
        self.Array = [NSMutableArray array];
    }
    return _Array;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self  = [super initWithFrame:frame];
    if (self) {
        [self creatSubview:frame];
        [self analysis];
    }
    return self;
}
-(void)analysis{
    [NetworkHandler getDataByURLString:@"http://api.liwushuo.com/v2/collections?limit=10&offset=0" BodyString:nil WithDataBlock:^(id result) {
        NSDictionary *dic = [result objectForKey:@"data"];
        NSMutableArray *arr = [dic objectForKey:@"collections"];
        for (NSDictionary *dic in arr) {
            StrategyUpModel *model = [StrategyUpModel modelWithDic:dic];
            [self.Array addObject:model];
            [self.collectionView reloadData];
        }
    }];
}


-(void)creatSubview:(CGRect)frame{
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.itemSize = CGSizeMake(SCREEN_WIDTH / 2.5, SCREEN_WIDTH / 4.5);
    flowLayout.minimumInteritemSpacing = 10;
    flowLayout.minimumLineSpacing = 10;
    flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10); // 内边距
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, SCREEN_WIDTH / 4 - SCREEN_WIDTH / 6  , SCREEN_WIDTH, SCREEN_HEIGHT / 6  ) collectionViewLayout:flowLayout];
    
    
    [self.collectionView jxl_setDayMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor whiteColor];
        
    } nightMode:^(UIView *view) {
        self.collectionView.backgroundColor = [UIColor grayColor];
        
        
    }
     ];
    
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self addSubview:_collectionView];
    [self.collectionView registerClass:[StrategyCollectionViewCell class] forCellWithReuseIdentifier:@"StrategyCell"];
//    self.collectionView.backgroundColor = UIColorSJ;
    
    
}


-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    return self.Array.count;
}

-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    self.StrategyCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"StrategyCell" forIndexPath:indexPath];
    self.StrategyCell.backgroundColor =  [UIColor colorWithRed:(arc4random()%256/255.0) green:(arc4random()%256/255.0) blue:(arc4random()%256/255.0) alpha:1.0];
    
    
    StrategyUpModel *model = self.Array[indexPath.item];
    self.StrategyCell.StrategyUpModel = model;
    
    return self.StrategyCell;
    
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (self.Array.count != 0) {
        StrategyUpModel *model = self.Array[indexPath.row];
        self.str = [NSString stringWithFormat:@"%ld",(long)model.Id];
        
    }
    
    [self.delegate didClickSelectItemAtIndexPath:indexPath str:self.str];
    
}




@end
